// pages/tools/commonQuery/commonQuery.js
const app = getApp();
const util = require("../../../utils/util");
var sIndex = "";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // allSearch: [{
        //   name: "政务办事",
        //   info: []
        // }, {
        //   name: "教育办事",
        //   info: []
        // }, {
        //   name: "交通出行",
        //   info: []
        // }, {
        //   name: "车辆服务",
        //   info: []
        // }, {
        //   name: "民生服务",
        //   info: []
        // }],
        allSearch: [{
            name: "办事查询",
            info: []
        }, {
            name: "交通查询",
            info: []
        }, {
            name: "教育查询",
            info: []
        }, {
            name: "便民查询",
            info: []
        }],
        count: 0,
        toView: '',
        toView1: '',
        menuIndex: 0,
        tabHeight: [],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        // 获取顶部搜索的距离
        var query = wx.createSelectorQuery()
        query.select('.scolls').boundingClientRect((res) => {
            this.setData({
                topHeight: res.height
            })

        }).exec()

        // 获取屏幕高度
        wx.getSystemInfo({
            success: (res) => {
                var clientHeight = res.windowHeight,
                    clientWidth = res.windowWidth,
                    rpxR = 750 / clientWidth;
                var calc = clientHeight * rpxR;
                this.setData({
                    winHeight: calc,
                    clientHeight,
                });
            }
        });
        util.getUrl(app.globalData.url).then((res) => {
            this.data.allSearch.forEach((v, k) => {
                this.getallSearch(v.name, k)
            })
        })
    },

    // 全部查询
    getallSearch(type, index) {
        let toolData = {
            action: "tools",
            city: wx.getStorageSync("city"),
            keywords: "",
            version: app.globalData.version,
            type,
        };
        util.request('service.php', toolData, ).then(res => {
            let resData = res.data.data;
            this.data.count++;
            this.data.allSearch[index].info = resData;
            this.setData({
                allSearch: this.data.allSearch
            })
            if (this.data.count == this.data.allSearch.length) {
                this.setData({
                    allSearch: this.data.allSearch
                })
                this.data.allSearch.forEach((v, k) => {
                    var query = wx.createSelectorQuery()
                    query.select('#L' + k + '.chaxun-box').boundingClientRect((res) => {
                        if (res) {
                            this.data.tabHeight.push(res.height)
                            this.setData({
                                tabHeight: this.data.tabHeight,
                            })
                        }
                    }).exec()
                })
            }
        })

    },

    menuClick(e) {
        let menuIndex = e.currentTarget.dataset.index;
        this.setData({
            menuIndex,
            toView1: 'L' + menuIndex
        })
    },

    scroll: function(e) {
        let scrollViewTop = e.detail.scrollTop;
        let tabHeight = this.data.tabHeight;
        var scroArr = [];
        var currNum = 0;
        tabHeight.forEach((item, index) => {
            if (index == 0) {
                scroArr.push(tabHeight[index]);
            } else {
                if (currNum) {
                    currNum = tabHeight[index] + currNum;
                } else {
                    currNum = tabHeight[index] + tabHeight[index - 1];
                }
                scroArr.push(currNum);
            }
        })

        let lastIndex = tabHeight.length - 1;
        for (let i = 0; i < scroArr.length; i++) {
            if (scrollViewTop >= 0 && scrollViewTop < scroArr[i]) {
                this.setData({
                    menuIndex: 0,
                    toView: 'L' + 0,
                })
                return;
            } else if (scrollViewTop >= scroArr[i] && scrollViewTop < scroArr[i + 1]) {
                this.setData({
                    menuIndex: i + 1,
                    toView: 'L' + (i + 1),
                })
                return;
            } else if (scrollViewTop >= scroArr[scroArr.length - 1]) {
                this.setData({
                    menuIndex: scroArr.length - 1,
                    toView: 'L' + (scroArr.length - 1),
                })
                return;
            }
        }
    },

    // 服务点击
    navDetail(e) {
        let path = e.currentTarget.dataset.path;
        let linkname = e.currentTarget.dataset.title;
        let appid = e.currentTarget.dataset.appid;
        if (path.indexOf("pages") == -1 && path.indexOf("mp.weixin.qq.com") == -1) {
            this.setData({
                showtk: true
            })
            util.navContact(linkname, path)
        } else {
            util.navMethod(path, appid);
        }
    },



    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})